#!/bin/sh
#fixrm is a little script to limit the items on each line so the line
#is less than 72 characters. it is used by the maintainer when creating
#the README.models file for this package, and for the epson-inkjet-printer-escpr2
#slackbuild. It takes the list posted on a single long first line, and spreads
#them over as many lines as needed.
#
#paste the list from the epson website into an empty README.models and run this
#script in the same directory

FNAME="README.models"
MAXLEN=${MAXLEN:-72}
#make sure MAXLEN is a integer bigger than 39
MAXLEN=`echo $MAXLEN|awk '($1<40) {print "40"} (int($1)>39) {print int($1) }'`
if [ -e $FNAME ]&&[ `cat $FNAME|head -n1|awk '{print length($0)}'` -gt $MAXLEN ]; then
  #only do something if the file exists, and thefirst line is longer than  it
  #should be.
  TMPFILE=rmtmp
  mv README.models $TMPFILE
  cat $TMPFILE|awk -v mlen="$MAXLEN" -F', ' 'BEGIN{curline="";mlen=mlen-2}
{ for (i=1;i<=NF; i++)
 { { n=0; l=length(curline)+length($i) }
   if (i==1) {curline=$i; n=1}
   if (l<mlen && i>1) { curline=curline ", " $i; n=1}
   if (n==0) {print curline; curline=$i }
 }
 if (length(curline)>0) {print curline; curline=""}
}'>README.models
  rm $TMPFILE
fi
